package com.party.core.service.mall.biz;

import com.google.common.collect.Sets;
import com.party.common.utils.StringUtils;
import com.party.core.model.BaseModel;
import com.party.core.model.mall.*;
import com.party.core.service.mall.IMallShowCategoryService;
import com.party.core.service.mall.IMallShowGoodsService;
import com.party.core.service.mall.IMallShowSubjectService;
import com.party.core.service.mall.IMallShowSubjectTemplateService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * @Description: 机构主题/主题分类的冗余字段统计
 * @Author: yangshoukun
 * @Date: 2018/4/12 19:23
 */
@Service
public class GoodsShowCountBizService {
    @Autowired
    private IMallShowSubjectTemplateService mallShowSubjectTemplateService;

    @Autowired
    private IMallShowSubjectService mallShowSubjectService;

    @Autowired
    private IMallShowGoodsService mallShowGoodsService;

    @Autowired
    private IMallShowCategoryService mallShowCategoryService;

    /**
     * 统计商品数
     * @param mallShowGoodsId
     */
    public void countGoodsNum(String mallShowGoodsId, boolean isAdd) {
        if (StringUtils.isEmpty(mallShowGoodsId)) {
            return;
        }
        MallShowGoods mallShowGoods = mallShowGoodsService.get(mallShowGoodsId);
        countGoodsNum(mallShowGoods, isAdd);
    }

    /**
     * 统计商品数
     * @param mallShowGoods
     */
    public void countGoodsNum(MallShowGoods mallShowGoods, boolean isAdd) {
        if (null == mallShowGoods) {
            return;
        }
        MallShowCategory mallShowCategory = mallShowCategoryService.get(mallShowGoods.getCategoryId());

        changeGoodsNum(mallShowCategory, isAdd, 1);
    }

    /**
     * 删除商品类型前, 统计商品数(该类型下的商品数全部减少)
     * @param categoryId
     */
    public void delGoodsNum(String categoryId) {
        if (StringUtils.isEmpty(categoryId)) {
            return;
        }

        MallShowGoods param = new MallShowGoods();
        param.setCategoryId(categoryId);
        List<MallShowGoods> goodsList = mallShowGoodsService.list(param);
        if (CollectionUtils.isEmpty(goodsList)) {
            return;
        }

        MallShowCategory mallShowCategory = mallShowCategoryService.get(categoryId);

        changeGoodsNum(mallShowCategory, false, goodsList.size());
    }

    /**
     * 增加机构数量--在保存之前
     * 减少机构数量--在删除之后
     * @param templateId
     * @param partnerId
     */
    public void addOrgNum(String templateId, String partnerId, boolean isAdd) {
        if (StringUtils.isEmpty(templateId) || StringUtils.isEmpty(partnerId)) {
            return;
        }
        MallShowSubjectTemplate template = mallShowSubjectTemplateService.get(templateId);
        boolean hasOrg = hasOrg(templateId, partnerId);
        if (hasOrg) {
            return;
        }
        changeOrgNum(template, isAdd, 1);
    }

    /**
     * 必须更新机构模板之前调用
     * @param oldMemberId
     * @param newMemberId
     * @param templateId
     */
    public void updateOrgNum(String oldMemberId, String newMemberId, String templateId) {
        if (StringUtils.isEmpty(templateId)) {
            return;
        }
        if (StringUtils.isEmpty(newMemberId)) {
            return;
        }
        if (newMemberId.equals(oldMemberId)) {
            return;
        }
        int count = 0;
        MallShowSubjectTemplate template = mallShowSubjectTemplateService.get(templateId);

        boolean hasOrg = hasOrg(templateId, newMemberId);
        count = hasOrg ? count : (count + 1);

        if (StringUtils.isNotEmpty(oldMemberId)) {
            MallShowSubject param = new MallShowSubject();
            param.setTemplateId(templateId);
            param.setMemberId(oldMemberId);
            List<MallShowSubject> subjectList = mallShowSubjectService.list(param);
            count = subjectList.size() > 1 ? count : (count - 1);
        }

        if (count == 0) {
            return;
        }

        changeOrgNum(template, true, count);
    }

    /**
     * 定时任务使用的矫正模板的冗余统计数
     */
    public void reviseTemplate(String templateId) {
        MallShowSubjectTemplate param = new MallShowSubjectTemplateQuery();
        param.setDelFlag(BaseModel.DEL_FLAG_NORMAL);
        if (StringUtils.isNotEmpty(templateId)) {
            param.setId(templateId);
        }
        List<MallShowSubjectTemplate> templateList = mallShowSubjectTemplateService.list(param);

        templateList.forEach(template -> {
            // 机构数
            MallShowSubject subjectParam = new MallShowSubject();
            subjectParam.setDelFlag(BaseModel.DEL_FLAG_NORMAL);
            subjectParam.setTemplateId(template.getId());
            List<MallShowSubject> subjectList = mallShowSubjectService.list(subjectParam);
            Set<String> memberIds = Sets.newHashSet();
            subjectList.forEach(subject -> {
                if (StringUtils.isNotEmpty(subject.getMemberId())) {
                    memberIds.add(subject.getMemberId());
                }
            });
            template.setOrgNum(memberIds.size());

            // 商品数
            int goodsNum = allGoodsNum(null, template.getId());
            template.setGoodsNum(goodsNum);

            // 更新
            mallShowSubjectTemplateService.update(template);
        });
    }

    /**
     * 定时任务矫正机构主题的冗余数
     * @param subjectId
     */
    public void reviseSubject(String subjectId, String memberId) {
        MallShowSubject subjectParam = new MallShowSubject();
        subjectParam.setDelFlag(BaseModel.DEL_FLAG_NORMAL);
        if (StringUtils.isNotEmpty(subjectId)) {
            subjectParam.setId(subjectId);
        }
        if (StringUtils.isNotEmpty(memberId)) {
            subjectParam.setMemberId(memberId);
        }
        List<MallShowSubject> subjectList = mallShowSubjectService.list(subjectParam);

        subjectList.forEach(subject -> {
            // 商品数
            int goodsNum = allGoodsNum(subject.getId(), null);
            subject.setGoodsNum(goodsNum);

            // 更新
            mallShowSubjectService.update(subject);
        });
    }
    /************************************************    私有方法  ******************************************************/

    /**
     * 统计主题(机构主题或主题模板)的商品数
     * @param subjectId
     * @param templateId
     * @return
     */
    private int allGoodsNum(String subjectId, String templateId) {
        int result = 0;
        if (StringUtils.isEmpty(subjectId) && StringUtils.isEmpty(templateId)) {
            return result;
        }
        MallShowCategory categoryParam = new MallShowCategory();
        categoryParam.setDelFlag(BaseModel.DEL_FLAG_NORMAL);
        if (StringUtils.isNotEmpty(templateId)) {
            categoryParam.setTemplateId(templateId);
        } else if (StringUtils.isNotEmpty(subjectId)) {
            categoryParam.setSubjectId(subjectId);
        }
        List<MallShowCategory> categoryList = mallShowCategoryService.list(categoryParam);
        for (MallShowCategory category: categoryList) {
            MallShowGoods goodsParam = new MallShowGoods();
            goodsParam.setDelFlag(BaseModel.DEL_FLAG_NORMAL);
            goodsParam.setCategoryId(category.getId());
            List<MallShowGoods> goodsList = mallShowGoodsService.list(goodsParam);
            if (goodsList.size() > 0) {
                result += goodsList.size();
            }
        }
        return result;
    }

    /**
     * 该模板下是否还有(某机构的)机构主题
     * @param templateId
     * @return
     */
    private boolean hasOrg(String templateId, String partnerId) {
        MallShowSubject param = new MallShowSubject();
        param.setTemplateId(templateId);
        if (StringUtils.isNotEmpty(partnerId)) {
            param.setMemberId(partnerId);
        }
        List<MallShowSubject> subjectList = mallShowSubjectService.list(param);
        return CollectionUtils.isNotEmpty(subjectList);
    }

    /**
     * 增加或减少机构数量
     * @param template
     * @param isAdd
     */
    private void changeOrgNum(MallShowSubjectTemplate template, boolean isAdd, Integer count) {
        count = count == null ? 1 : count;
        Integer num = template.getOrgNum();
        num = num == null ? 0 : num;
        num = isAdd ? (num + count) : (num - count);
        template.setOrgNum(num);
        mallShowSubjectTemplateService.update(template);
    }

    /**
     * 增加或减少商品数
     * @param mallShowCategory
     * @param isAdd
     * @param count
     */
    private void changeGoodsNum(MallShowCategory mallShowCategory, boolean isAdd, Integer count) {
        count = count == null ? 1 : count;
        if (StringUtils.isNotEmpty(mallShowCategory.getSubjectId())) {
            MallShowSubject mallShowSubject = mallShowSubjectService.get(mallShowCategory.getSubjectId());
            Integer num = mallShowSubject.getGoodsNum();
            num = num == null ? 0 : num;
            num = isAdd ? (num + count) : (num - count);
            mallShowSubject.setGoodsNum(num);
            mallShowSubjectService.update(mallShowSubject);
        } else if (StringUtils.isNotEmpty(mallShowCategory.getTemplateId())) {

            MallShowSubjectTemplate mallShowSubjectTemplate = mallShowSubjectTemplateService.get(mallShowCategory.getTemplateId());
            Integer num = mallShowSubjectTemplate.getGoodsNum();
            num = num == null ? 0 : num;
            num = isAdd ? (num + count) : (num - count);
            mallShowSubjectTemplate.setGoodsNum(num);
            mallShowSubjectTemplateService.update(mallShowSubjectTemplate);
        }
    }
}
